package com.example.myprojectoffeixue;

import android.app.Activity;
import android.content.SharedPreferences;
import android.widget.GridView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

public class MyGateWayActivity extends Activity {
    //我的网关界面
    private GridView gd;
    private String socketToken;
    private String userId;
    private MyApplication Myapp;

    private void GetDeviceList() {
        Myapp = (MyApplication) getApplication();
        SharedPreferences sp = getSharedPreferences("first", MODE_PRIVATE);
        String UserName = sp.getString("username", null);
        if (UserName != null && !"".equals(UserName)) {
            GateWay Gw = new GateWay();
            Gw.mobile = UserName;
            Gw.status = 1;
            String j = JSON.toJSONString(Gw);
            j = AESUtil.encrypt(j);
            HttpUtils httpUtils = new HttpUtils();
            RequestParams params = new RequestParams();
            params.setHeader("Content-Type", "application/json");
            try {
                params.setBodyEntity(new StringEntity(j, "utf-8"));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            httpUtils.send(HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/device/getDeviceList", params,
                    new RequestCallBack<String>() {

                        @Override
                        public void onFailure(HttpException arg0, String arg1) {
                            HttpConnection.toastLog("网络异常", MyGateWayActivity.this);

                        }

                        @Override
                        public void onSuccess(ResponseInfo<String> arg0) {
                            if (arg0.result != null && !("".equals(arg0.result))) {
                                arg0.result = AESUtil.decrypt(arg0.result);
                                JSONObject jb = JSONObject.parseObject(arg0.result);
                                JSONArray ja = jb.getJSONArray("deviceList");
                                Myapp.m_gateway = (ArrayList<GateWay>) JSON.parseArray(ja.toString(), GateWay.class);
                            } else {
                                HttpConnection.toastLog("服务器错误", MyGateWayActivity.this);

                            }
                        }

                    });

        }
    }


/*
        gd.setOnItemLongClickListener(new OnItemLongClickListener() {

            Dialog dialog;

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {

                dialog = new Dialog(MyGateWayActivity.this);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.wgdialog);
                dialog.show();
                TextView delete = (TextView) dialog.findViewById(R.id.delete);
                TextView restart = (TextView) dialog.findViewById(R.id.restart);
                TextView reset = (TextView) dialog.findViewById(R.id.reset);
                ImageView iv = (ImageView) dialog.findViewById(R.id.iv1_mydialog);

                iv.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                delete.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        GateWay Gw = new GateWay();
                        if(Mygateway.size()!=0){
                        Gw.id = Mygateway.get(position).id;}
                        String j = JSON.toJSONString(Gw);
                        j=AESUtil.encrypt(j);
                        HttpUtils httpUtils = new HttpUtils();
                        RequestParams paraMyappyapps = new RequestParams();
                        paraMyappyapps.setHeader("Content-Type", "application/json");
                        try {
                            paraMyappyapps.setBodyEntity(new StringEntity(j, "utf-8"));
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        httpUtils.send(HttpMethod.POST, "http://"+UserClass.ServerIP+"/FXMobiServer/device/deleteDevice", paraMyappyapps,
                                new RequestCallBack<String>() {

                                    @Override
                                    public void onFailure(HttpException arg0, String arg1) {
                                        HttpConnection.toastLog("网络异常",MyGateWayActivity.this);
                                    }

                                    @Override
                                    public void onSuccess(ResponseInfo<String> arg0) {

                                        if (arg0.result != null && !("".equals(arg0.result))) {
                                           arg0.result=AESUtil.decrypt(arg0.result);
                                           JSONObject j = JSONObject.parseObject(arg0.result);
                                           Integer it = j.getInteger("status");
                                           if (0 == it) {
                                            SharedPreferences sp = getSharedPreferences("first", MODE_PRIVATE);
                                            String Mac = sp.getString("Mac", null);
                                            if (Mac != null && Mygateway.get(position).mac.equals(Mac)) {
                                                sp.edit().putString("Mac", null).commit();
                                            }
                                            if(Mygateway.size()!=0){
                                            Mygateway.remove(position);
                                            Gwadapter.notifyDataSetChanged();
                                            dialog.dismiss();
                                            }
                                        }
                                    }else{

                                            HttpConnection.toastLog("服务器错误",MyGateWayActivity.this);

                                        }
                                    }
                                });


                    }
                });


                restart.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        Myapp.mac = Mygateway.get(position).mac;
                        Myapp.connect();
                        AlertDialog.Builder builder = new AlertDialog.Builder(MyGateWayActivity.this);
                        builder.setMessage("正在重启网关");
                        builder.setTitle("提示");
                        final Dialog dialog1 = builder.create();
                        dialog1.show();
                        new Thread() {
                            @Override
                            public void run() {
                                while (true) {
                                    if (Myapp.flag == 1) {
                                        SheBeiClass sk = new SheBeiClass("1004");
                                        String t = JSON.toJSONString(sk);
                                        Myapp.c.send(t);
                                        dialog1.dismiss();
                                        break;

                                    }

                                }


                            }
                        }.start();


                    }
                });


                reset.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        dialog.dismiss();
                        Myapp.mac = Mygateway.get(position).mac;
                        Myapp.connect();
                        AlertDialog.Builder builder = new AlertDialog.Builder(MyGateWayActivity.this);
                        builder.setMessage("正在恢复出厂设置");
                        builder.setTitle("提示");
                        final Dialog dialog1 = builder.create();
                        dialog1.show();

                        new Thread() {
                            @Override
                            public void run() {
                                while (true) {
                                    if (Myapp.flag == 1) {
                                        SheBeiClass sk = new SheBeiClass("1003");
                                        String t = JSON.toJSONString(sk);
                                        Myapp.c.send(t);
                                        dialog1.dismiss();
                                        break;
                                    }
                                }
                            }
                        }.start();
                    }
                });
                return true;

            }


        });


        gd.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent it = new Intent(MyGateWayActivity.this, MainActivity.class);
                if(Mygateway.size()!=0){
                Myapp.mac = Mygateway.get(position).mac;
                Myapp.lsb.clear();
                Myapp.lsb1.clear();
                Myapp.lsb2.clear();
                Myapp.lsb3.clear();
                Myapp.lsb4.clear();
                Myapp.lsb5.clear();
                Myapp.lsb6.clear();
                Myapp.lsb7.clear();
                Myapp.lsb8.clear();
                startActivity(it);}
            }
        });*/


}
